﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace blackhouse.BlackApart.Data
{
    public abstract class AdoPlaceDataAccess : IPlaceDataAccess
    {

        #region Methods

		public PlaceData[] GetAllPlaces(string language)
        {
            return this.ReadPlaces(this.GetReaderForAllPlaces(language));
        }

		protected abstract IDataReader GetReaderForAllPlaces(string language);

        private PlaceData[] ReadPlaces(IDataReader reader)
        {
            List<PlaceData> data = new List<PlaceData>();
            while (reader.Read())
                data.Add(this.ReadPlaceFields(reader));
            reader.Close();
            return data.ToArray();
        }

        private PlaceData ReadPlaceFields(IDataReader reader)
        {
            PlaceData data = new PlaceData();
            if (!reader.IsDBNull(reader.GetOrdinal("Address"))) data.Address = Convert.ToString(reader["Address"]);
            if (!reader.IsDBNull(reader.GetOrdinal("Description"))) data.Description = Convert.ToString(reader["Description"]);
            data.Category = Convert.ToString(reader["Category"]);
            data.LocationLatitude = Convert.ToDouble(reader["LocationLatitude"]);
            data.LocationLongitude = Convert.ToDouble(reader["LocationLongitude"]);
            data.Name = Convert.ToString(reader["Name"]);
            data.PlaceId = Convert.ToInt32(reader["PlaceId"]);
            return data;
        }

        #endregion

    }
}
